package com.dawnling.amapinfowindows;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

/**
 * 跳转高德地图导航
 * Created by dawning on 2018/04/28.
 */
public class OpenGaoDeMap {

    /**
     * 检查手机上是否安装了指定的软件
     * @param context
     * @param packageName：应用包名
     * @return
     */
    private static boolean isAvilible(Context context, String packageName){
        //获取packagemanager
        final PackageManager packageManager = context.getPackageManager();
        //获取所有已安装程序的包信息
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        //用于存储所有已安装程序的包名
        List<String> packageNames = new ArrayList<String>();
        //从pinfo中将包名字逐一取出，压入pName list中
        if(packageInfos != null){
            for(int i = 0; i < packageInfos.size(); i++){
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }
        }
        //判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        return packageNames.contains(packageName);
    }


    public static void openGaoDeMap(Context context, String lotName, String lat, String lon){
        if(isAvilible(context,"com.autonavi.minimap")){
            try{
                Intent intent = Intent.getIntent("androidamap://viewMap?sourceApplication=" + AppUtils.getAppName() +
                        "&poiname=" + lotName +
                        "&lat=" + lat +
                        "&lon=" + lon +
                        "&dev=0");
                context.startActivity(intent);
            } catch (URISyntaxException e){
                e.printStackTrace();
            }
        }else {
            ToastUtils.showShort("请前往应用市场下载高德地图");
            return;
        }
    }
}
